
//
//  ZHVisitorView.swift
//  999999新浪微博
//
//  Created by 陈华 on 15/12/16.
//  Copyright © 2015年 华锐. All rights reserved.
//

import UIKit

protocol ZHVisitorViewDelegate : NSObjectProtocol {
    
    func visitorViewLoginClick()
}

class ZHVisitorView: UIView{
    //代理的属性写法
    weak var delegate: ZHVisitorViewDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()//准备UI
        backgroundColor = UIColor(red: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 准备UI
    func prepareUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(shadeImageView)//遮罩层添加一定要注意顺序
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerBnt)
        addSubview(lonInBnt)
        
        //添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        shadeImageView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBnt.translatesAutoresizingMaskIntoConstraints = false
        lonInBnt.translatesAutoresizingMaskIntoConstraints = false
        
       // iconView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -40))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        addConstraint(NSLayoutConstraint(item: shadeImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: shadeImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: shadeImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: shadeImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: -295))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: -120))
        
        addConstraint(NSLayoutConstraint(item: registerBnt, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: registerBnt, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBnt, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBnt, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40))
        
        addConstraint(NSLayoutConstraint(item: lonInBnt, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: lonInBnt, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: lonInBnt, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: lonInBnt, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40))
        
        
        //其它设置
        messageLabel.textColor = UIColor.darkGrayColor()
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = NSTextAlignment.Center//文本中心对齐
        
        registerBnt.setTitle("注册", forState: UIControlState.Normal)
        registerBnt.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        lonInBnt.setTitle("登陆", forState: UIControlState.Normal)
        lonInBnt.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        shadeImageView.hidden = true 
    }
    
    //MARK: - 供外界调用创建view的方法
    func loadView(iconImage: String, homeImage: String?, message: String){
        iconView.image = UIImage(named: iconImage)
        if let h = homeImage {
            homeView.image = UIImage(named: h)//可选是说h可能是nil
        }
        messageLabel.text = message
    }
    
    //MARK: iconView旋转的方法
    func iconViewRotate(){
       let basic = CABasicAnimation(keyPath: "transform.rotation")
        basic.fromValue = 0
        basic.toValue = M_PI * 2
        basic.repeatCount = MAXFLOAT
        basic.duration = 20
        basic.removedOnCompletion = false //动画完成后时不宜除
        iconView.layer.addAnimation(basic, forKey: nil)
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复动画
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }

    func clickLogIn(){
        
        delegate?.visitorViewLoginClick()
    }
    
       
    //MARK: - 创建控件：懒加载
    private lazy var iconView = UIImageView()
    lazy var shadeImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private  lazy var homeView = UIImageView()
    private  lazy var messageLabel = UILabel()
    private  lazy var registerBnt: UIButton = {
        let bnt = UIButton(type: UIButtonType.Custom)
        bnt.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return bnt
    }()
    private  lazy var lonInBnt: UIButton = {
        let bnt = UIButton(type: UIButtonType.Custom)
        bnt.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        bnt.addTarget(self, action: "clickLogIn", forControlEvents: UIControlEvents.TouchUpInside)
        return bnt
    }()
}


















